package softtech.apps.callblocker.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.CallBlockedItem;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AdapterCallHistoryInformation extends BaseAdapter{

	LayoutInflater inf;
	Context mContext;
	ArrayList<CallBlockedItem> mListInfo = new  ArrayList<CallBlockedItem>();
	//display date
    LinearLayout.LayoutParams lparamDate;
    //display date and number
    LinearLayout.LayoutParams lparamDateNumber;
    int mode=0;
	public AdapterCallHistoryInformation(ArrayList<CallBlockedItem> item, LayoutInflater inf, Context mcontext)
	{
		this.inf = inf;
		this.mContext = mcontext;
		this.mListInfo = item;
		if(item.size()>0){
		mode = item.get(0).getStatus();
		}
	}
	int count = 10;
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(mListInfo.size()>count){
			return count;
		}
		return mListInfo.size()+1;
	}
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
		ViewHolder viewholder= null;
		if(vi==null){
			vi = inf.inflate(R.layout.item_call_history_information_item, null);
			viewholder = new ViewHolder();
			createComponent(viewholder,vi);
			vi.setTag(viewholder);
			
		}else{
			viewholder = (ViewHolder) vi.getTag();
		}
		 if(position==0)
         {
        	//display date 
        	 fShowDate(viewholder,position);
         }
         else {
        	 fShowNumberDate(position-1,viewholder);
         }
		return vi;
	}
	class ViewHolder{
		/**
		 * show day time
		 */
		TextView tvdate;
		/**
		 * show phone number
		 */
		TextView tvPhoneNumber;
		/**
		 * Show mode call
		 */
		ImageView ivMode;
		/**
		 * Layout of item
		 */
		RelativeLayout relativeLayout;
	}
	/**
	 * set init component
	 * @param view
	 */
	private void createComponent(ViewHolder view,View vi){
		// display date
					lparamDate = new LinearLayout.LayoutParams(0, 0);
					// display date and number
					lparamDateNumber = new LinearLayout.LayoutParams(
							WindowManager.LayoutParams.WRAP_CONTENT,
							WindowManager.LayoutParams.WRAP_CONTENT);
		view.tvdate = (TextView) vi.findViewById(R.id.tvRecentdate);
		view.tvPhoneNumber = (TextView)vi.findViewById(R.id.tvRecentCall);
		view.ivMode = (ImageView)vi.findViewById(R.id.ivInfoCall);
		view.relativeLayout = (RelativeLayout)vi.findViewById(R.id.layoutInfoHistoryItem);
		
	}
	/**
	 * show section header day
	 */
	/**
	 * convert datetime to date
	 * @param date
	 */
	private String stringToDate(String day){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(day);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("yyyy-MM-dd");  
		String day1=null;
		try {  
		  
		 day1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return day1;
	}
	/**
	 * convert datetime to time
	 * @param time
	 * @return
	 */
	private String stringToTime(String time){
		Date date = null;
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
		try {  
		     date = format.parse(time);  
		    System.out.println(date);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		SimpleDateFormat  dateformat = new SimpleDateFormat("HH:mm");  
		String time1 =null;
		try {  
		  
		 time1= dateformat.format(date);
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return time1;
	}
	/**
	 * Show section date
	 * 
	 */
	private void fShowDate(ViewHolder view , int id){
		 view.tvPhoneNumber.setLayoutParams(lparamDate);
    	 view.tvdate.setLayoutParams(lparamDateNumber);
    	 view.tvdate.setTypeface(Typeface.DEFAULT_BOLD);
    	 view.tvdate.setTextSize(15);
    	 view.tvdate.setFocusable(true);
    	 view.ivMode.setLayoutParams(lparamDate);
    	 //
    	 // get datetime from callblockeditem
    	 //
    	 String getdate = mListInfo.get(id).getDate_time();
    	 SimpleDateFormat dfdate = new SimpleDateFormat("yyyy-MM-dd");
    	 //
    	 // get today
    	 //
    	 Calendar cToday = Calendar.getInstance();
    	 String today = dfdate.format(cToday.getTime());
    	 //
    	 // get yesterday
    	 //
    	 Calendar cYesterday = Calendar.getInstance();
    	 cYesterday.add(Calendar.DATE, -1);
    	 String yesterday = dfdate.format(cYesterday.getTime());
    	 if(today.equals(stringToDate(getdate)))
    	 {
    		 view.tvdate.setText("Today");
    	 }
    	 else if(yesterday.equals(stringToDate(getdate))){
    		 view.tvdate.setText("Yesterday");
    	 }
    	 else {
    		 view.tvdate.setText(""+stringToDate(getdate));
    	 }
    	 view.relativeLayout.setBackgroundColor(Color.parseColor("#1d1e23"));
	}
	/**
	 * Show phone number and date
	 * 
	 * recall
	 */
	private void fShowNumberDate(int position,ViewHolder view){
		 view.ivMode.setLayoutParams(lparamDateNumber);
    	 view.tvPhoneNumber.setLayoutParams(lparamDateNumber);
    	 view.tvPhoneNumber.setText(""+mListInfo.get(position).getPhoneNumber());
    	 view.tvdate.setTextSize(10);
    	 view.tvdate.setFocusable(false);
    	 
    	 String time = mListInfo.get(position).getDate_time();
    	 
    	 view.tvdate.setText("Time: "+stringToTime(time));
    	 view.relativeLayout.setBackgroundColor(Color.parseColor("#3d3f4b"));
    	 switch (mode) {
 		case 16:
 			view.ivMode.setBackgroundResource(R.drawable.modecall1);
 			break;
 		case 32:
 			view.ivMode.setBackgroundResource(R.drawable.modecall1);
 			break;
 		case 48:
 			view.ivMode.setBackgroundResource(R.drawable.modecall3);
 			break;
 		case 64:
 			view.ivMode.setBackgroundResource(R.drawable.modecall3);
 			break;
 		case 80:
 			view.ivMode.setBackgroundResource(R.drawable.modecall5);
 			break;
 		case 96:
 			view.ivMode.setBackgroundResource(R.drawable.modecall5);
 			break;

 		default:
 			break;
 		}
	}
	/**
	 * Recall
	 * @param phonenumber
	 */
	private void fRecall(String phonenumber){
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		callIntent.setData(Uri.parse("tel:"+phonenumber));
		mContext.startActivity(callIntent);
	}
}
